package xyz.labmem.app.unit

import javafx.animation.KeyFrame
import javafx.animation.KeyValue
import javafx.animation.Timeline
import javafx.beans.property.SimpleDoubleProperty
import javafx.event.EventHandler
import javafx.geometry.Insets
import javafx.geometry.Pos
import javafx.scene.Node
import javafx.scene.image.ImageView
import javafx.scene.layout.AnchorPane
import javafx.scene.layout.HBox
import javafx.scene.layout.VBox
import javafx.scene.paint.Color
import javafx.util.Duration
import xyz.labmem.app.config.AppConfig
import xyz.labmem.app.config.AppConfig.mouseImage
import xyz.labmem.app.config.AppConfig.uiImage
import xyz.labmem.app.main.MainStage
import xyz.labmem.app.util.BackgroundMusic
import xyz.labmem.app.util.Lines
import xyz.labmem.app.util.dragWin


class TopDrag(stage: MainStage) : VBox() {

    init {
        val msgBox = HBox()
        children.addAll(msgBox)
        prefWidthProperty().bind(stage.root.prefWidthProperty())
        val msgUnit = MsgUnit.getMsgUnit()
        msgBox.children.apply children@{
            val icon = ImageView(AppConfig.portraitsImage["mona"]!!).apply {
                isPreserveRatio = true
                fitHeightProperty().bind(stage.root.prefHeightProperty().divide(5))
                setMargin(this, Insets(0.0, 0.0, 0.0, 10.0))
                this@children.add(this)
                msgUnit.onMouseClicked = EventHandler {
                    msgUnit.isVisible = false
                }
                setOnMouseClicked {
                    BackgroundMusic.mona1Play()
                    msgUnit.showMsg(Lines.getMonaLineByRandom())
                }
                downAction(this, 40)
                val yc = SimpleDoubleProperty(40.0)
                stage.dragWin(this, yc = yc)
            }
            val close = ImageView(uiImage["close"]!!).apply {
                isPreserveRatio = true
                fitHeightProperty().bind(stage.root.prefHeightProperty().divide(10))
                alignment = Pos.BOTTOM_CENTER
                downAction(this, 100)
                val action = Timeline(
                    KeyFrame(
                        Duration.seconds(.4),
                        KeyValue(this.rotateProperty(), 30),
                    ),
                    KeyFrame(
                        Duration.seconds(.8),
                        KeyValue(this.rotateProperty(), 0),
                    ),
                )
                action.cycleCount = Timeline.INDEFINITE
                onMouseEntered = EventHandler {
                    MainStage.setCursor(mouseImage["m6"])
                    Thread.sleep(100)
                    action.play()
                }
                onMouseExited = EventHandler {
                    MainStage.cursorBack()
                    rotate = 0.0
                    action.stop()
                }
            }
            AnchorPane().apply {
                prefWidthProperty().bind(
                    stage.root.prefWidthProperty().subtract(icon.fitHeightProperty())
                        .subtract(close.fitHeightProperty()).subtract(40)
                )
                prefHeightProperty().bind(icon.fitHeightProperty().divide(1.5))
                msgUnit.isVisible = false
                children.add(msgUnit)
                this@children.add(this)
            }
            this@children.add(close)
            close.setOnMouseClicked {
                stage.close()
                System.gc()
            }
        }
        children.add(
            AnchorPane().apply a@{
                this.prefWidthProperty().bind(stage.root.prefWidthProperty())
                prefWidthProperty().addListener { _, _, value ->
                    style = """
                    -fx-background-image: url("image/ui/t_bg.png");
                    -fx-background-size: ${value}px;
                    """.trimIndent()
                }
                prefHeight = 50.0
                val yc = SimpleDoubleProperty()
                yc.bind(msgBox.heightProperty())
                stage.dragWin(this, yc = yc)
                LabelUnit(
                    AppConfig.appName, 25.0,
                    color = Color.web("#ff0022"),
                    backColor = Color.web("#ff0022"),
                    oColor = Color.BLACK,
                    rotateS = -8.0,
                    rotateE = 8.1
                ).apply {
                    AnchorPane.setRightAnchor(this, 60.0)
                    AnchorPane.setBottomAnchor(this, 5.0)
                    this@a.children.add(this)
                }
                val star = ImageView(uiImage["star"])
                star.fitHeight = 40.0
                star.fitWidth = 40.0
                star.onMouseClicked = EventHandler {
                    BackgroundMusic.played()
                }
                star.onMouseEntered = EventHandler {
                    MainStage.setCursor(mouseImage["m6"])
                }
                star.onMouseExited = EventHandler {
                    MainStage.cursorBack()
                }
                AnchorPane.setRightAnchor(star, 10.0)
                AnchorPane.setBottomAnchor(star, 2.0)
                this@a.children.add(star)
                val tl = Timeline(
                    KeyFrame(
                        Duration.seconds(.3), {
                            star.image = uiImage["star"]
                            star.rotate = 0.0
                        }
                    ),
                    KeyFrame(
                        Duration.seconds(.6), {
                            star.image = uiImage["star1"]
                            star.rotate = 20.0
                        }
                    ),
                    KeyFrame(
                        Duration.seconds(.9), {
                            star.image = uiImage["star2"]
                            star.rotate = 40.0
                        }
                    ),
                )
                tl.cycleCount = Timeline.INDEFINITE
                tl.play()
            }
        )
    }

    private fun downAction(node: Node, px: Int) {
        Timeline(
            KeyFrame(
                Duration.millis(500.0),
                KeyValue(node.translateYProperty(), px)
            ), // 向下移动
        ).play()
    }

}